/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY. 
 * See the GNU General Public License for more details.
 * 
 * Copyright (C) 2011 CellObject.net
 */
package net.cellobject.javacodeformatter.editor;

import java.io.PrintWriter;

/**
 * A key/value pair in the {@link PropertyFile}.
 */
public class PropertyEntry extends PropertyElement {
	String key;

	String value;

	public PropertyEntry(final PropertyCategory parent, final String key, final String value) {
		super(parent);
		this.key = key;
		this.value = value;
	}

	public String getKey() {
		return key;
	}

	public String getValue() {
		return value;
	}

	@Override
	public PropertyElement[] getChildren() {
		return NO_CHILDREN;
	}

	public void setKey(final String text) {
		if (key.equals(text)) {
			return;
		}
		key = text;
		((PropertyCategory)getParent()).keyChanged(this);
	}

	public void setValue(final String text) {
		if (value.equals(text)) {
			return;
		}
		value = text;
		((PropertyCategory)getParent()).valueChanged(this);
	}

	@Override
	public void removeFromParent() {
		((PropertyCategory)getParent()).removeEntry(this);
	}

	public void appendText(final PrintWriter writer) {
		writer.print(key);
		writer.print(" = ");
		writer.println(value);
	}
}
